package com.workcell.workdata.xthings.controller;

import com.github.pagehelper.PageInfo;
import com.workcell.workdata.common.core.util.R;
import com.workcell.workdata.xthings.dto.PageVo;
import com.workcell.workdata.xthings.dto.TenantDto;
import com.workcell.workdata.xthings.dto.TenantVo;
import com.workcell.workdata.xthings.entity.Tenant;
import com.workcell.workdata.xthings.service.TenantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.List;

@Api(value = "TenantController", tags = "租户管理")
@RestController
@RequestMapping("/tenant")
@RequiredArgsConstructor
public class TenantController {
    private final TenantService tenantService;

    @ApiOperation("租户列表")
    @GetMapping("/list")
    public R<List<TenantDto>> tenantList(TenantVo tenantVo) {
        List<TenantDto> list = tenantService.tenantList(tenantVo);
        return R.ok(list);
    }

    @ApiOperation("租户分页")
    @GetMapping("/page")
    public R<PageInfo<TenantDto>> tenantPage(PageVo pageVo, TenantVo tenantVo) {
        PageInfo<TenantDto> pageInfo = tenantService.tenantPage(pageVo, tenantVo);
        return R.ok(pageInfo);
    }

    @ApiOperation("租户保存")
    @PostMapping("/insert")
    public R<Boolean> insert(@Valid @RequestBody TenantVo tenantVo) {
        return R.ok(tenantService.insert(tenantVo));
    }

    @ApiOperation("租户删除")
    @DeleteMapping("/delete/{tenantCode}")
    public R<Boolean> delete(@Valid @NotEmpty(message = "厂商ID不能为空")
                             @ApiParam(value = "厂商ID")
                             @PathVariable("tenantCode") String tenantCode) {
        return R.ok(tenantService.delete(tenantCode));
    }

    @ApiOperation("租户下拉列表")
    @GetMapping("/selectList")
    public R<List<Tenant>> selectList(TenantVo tenantVo) {
        return R.ok(tenantService.selectList(tenantVo));
    }
}
